import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.PerfCounterInfo;
import com.vmware.vim25.PerfEntityMetric;
import com.vmware.vim25.PerfEntityMetricBase;
import com.vmware.vim25.PerfMetricId;
import com.vmware.vim25.PerfMetricIntSeries;
import com.vmware.vim25.PerfMetricSeries;
import com.vmware.vim25.PerfProviderSummary;
import com.vmware.vim25.PerfQuerySpec;
import com.vmware.vim25.PerfSampleInfo;
import com.vmware.vim25.VirtualMachineRuntimeInfo;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedObject;
import com.vmware.vim25.mo.PerformanceManager;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

class test {
	
	static final String SERVER_NAME = "192.168.0.7";
	static final String USER_NAME = "root";
	static final String PASSWORD = "VMkk120";
	private static final String HOSTNAME = "10.16.2.32";
	private static final int SELECTED_COUNTER_ID = 1; // Active (mem) in KB (absolute)

	public static Map<String, PerfCounterInfo> getPerfCounters(
			PerformanceManager pm) throws Exception {
		Map<String, PerfCounterInfo> result = new HashMap<String, PerfCounterInfo>();
		PerfCounterInfo[] counters = pm.getPerfCounter();
		if (counters != null) {
			for (PerfCounterInfo counter : counters) {
				String key = counter.getGroupInfo().getKey() + "."
						+ counter.getNameInfo().getKey() + "."
						+ counter.getRollupType().toString();
				result.put(key, counter);
			}
		}
		return result;
	}

	public static void main(String  args[])throws Exception{
		
		String url = "https://" + SERVER_NAME + "/sdk/vimService";
	ServiceInstance si = new ServiceInstance(new URL(url), USER_NAME, PASSWORD, true);
		
    VirtualMachine host = (VirtualMachine) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("VirtualMachine","重庆数据库服务器");
    VirtualMachineRuntimeInfo info = host.getRuntime();
    
    int usage = info.getMaxCpuUsage();
//    
    int me = info.getMaxMemoryUsage();
//    
    System.out.println(usage+"--"+me);
    
	ManagedObjectReference me1=host.getMOR();
	PerformanceManager pm = si.getPerformanceManager();
	
	PerfCounterInfo[] pi = pm.queryPerfCounterByLevel(4);
	
	Map<String, PerfCounterInfo> result = new HashMap<String, PerfCounterInfo>();
	
	for(PerfCounterInfo p :pi){
		
		String key = p.getGroupInfo().getKey() + "."
				+ p.getNameInfo().getKey() + "."
				+ p.getRollupType().toString();
		
		System.out.println(key);
		result.put(key, p);
	}
	Map<String, PerfCounterInfo> counters = result;
	PerfProviderSummary summary = pm.queryPerfProviderSummary(host);
	int perfInterval = summary.getRefreshRate();
	System.out.println("Refresh rate = " + perfInterval);
	PerfMetricId metric0 = new PerfMetricId();
	
	metric0.setCounterId(counters.get("mem.usage.maximum").getKey());
	
	metric0.setInstance("");
	
	PerfQuerySpec qSpec = new PerfQuerySpec();
	
	qSpec.setMetricId(new PerfMetricId[]{metric0});
	
	qSpec.intervalId = perfInterval;
	
	qSpec.setFormat("normal");
	
	qSpec.setEntity(me1);
	
	PerfEntityMetricBase[] pembs = pm.queryPerf(new PerfQuerySpec[] {qSpec});
	
	System.out.println("aa");
	
	for (int i = 0; pembs != null && i < pembs.length; i++) {
		PerfEntityMetricBase val = pembs[i];
		PerfEntityMetric pem = (PerfEntityMetric) val;
		PerfMetricSeries[] vals = pem.getValue();
		PerfSampleInfo[] infos = pem.getSampleInfo();

		for (int j = 0; vals != null && j < vals.length; ++j) {
			PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j];
			long[] longs = val1.getValue();
			for (int k = 0; k < longs.length; k++) {
				System.out.println(infos[k].getTimestamp().getTime() + " : " + longs[k]);
			}
			System.out.println();
		}
	}
}
}